// BaseMethodPerformer.cpp: implementation of the BaseMethodPerformer class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "kwclientcore.h"
#include "BaseMethodPerformer.h"

#include "CommonLib\Packet.h"

#include "CommClientLib\CommWithServer.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

BaseMethodPerformer::BaseMethodPerformer(BaseMethodCallback * pCallback):
m_pCallback(pCallback),
m_pPacket(NULL),
m_pResult(NULL),
m_bCancel(false)
{
  ASSERT(pCallback);
}

BaseMethodPerformer::~BaseMethodPerformer()
{
  if (m_pPacket) delete m_pPacket;
  if (m_pResult) delete m_pResult;
}

void BaseMethodPerformer::SetPerformingData(void * pData, int nDataSize)
{
  if (m_pPacket) delete m_pPacket;
  m_pPacket = new Packet(nDataSize, (LPBYTE)pData);
}

int BaseMethodPerformer::Performing()
{
  m_pResult = new Packet;
  m_pResult->m_dwId = CommWithServer::Instance()->SendRequest(*m_pPacket);
  m_bCancel = false;
  if (!CommWithServer::Instance()->Receive(*m_pResult, m_bCancel, 100))
  {
    ASSERT(!m_pResult->GetpData());
  }

  return 0;
}

void * BaseMethodPerformer::GetpResult() const
{
  ASSERT(m_pResult);
  return m_pResult->GetpData();
}
